var app = angular.module('ZJPC');
app.controller('BuyRecordCtrl', ['$scope','$state','$rootScope','$stateParams','DataService','$filter',BuyRecordCtrl]);

function BuyRecordCtrl($scope,$state,$rootScope,$stateParams,DataService,$filter) {

	$scope.carFile = {name:$stateParams.xm+'('+$stateParams.jid+')'}

	function callback(data){
		$scope.loading = false;
		$scope.list_origin = data;
		$scope.pageOption = {
			itemCount :data.length,
			pageSize:10  
		}
	}

	$scope.searchTrack = function(){
		$scope.loading= true;
		DataService.getxfjl($filter('date')($scope.starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')($scope.endtime,'yyyy-MM-dd HH:mm:ss'),$stateParams.gid,callback)
	}

	$scope.searchTrack();

	$scope.clickLi = function(id){
		/*ModalService.openModalTp(id,)*/
	}

	$scope.getData = function(item){
		$scope.list = $scope.list_origin.slice(item[0]-1,item[1])
	}

	$scope.endtime = new Date();
	$scope.starttime = new Date().setHours(0,0,0);

			/*时间控件校验相关设置*/
	$scope.endDateBeforeRender = endDateBeforeRender
	$scope.endDateOnSetTime = endDateOnSetTime
	$scope.startDateBeforeRender = startDateBeforeRender
	$scope.startDateOnSetTime = startDateOnSetTime

	function startDateOnSetTime () {
	  $scope.$broadcast('start-date-changed');
	}

	function endDateOnSetTime () {
	  $scope.$broadcast('end-date-changed');
	}

	function startDateBeforeRender ($dates) {
	  if ($scope.endtime) {
	    $scope.endtime = new Date($scope.endtime).setHours(23,59,59)
	    var activeDate = moment($scope.endtime);

	    $dates.filter(function (date) {
	      return date.localDateValue() >= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}

	function endDateBeforeRender ($view, $dates) {
	  if ($scope.starttime) {
	    var activeDate = moment($scope.starttime).subtract(1, $view).add(1, 'minute');

	    $dates.filter(function (date) {
	      return date.localDateValue() <= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}
	
}